package com.xinqi.modules.course.clazz.controller.web;

import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.base.response.R;
import com.xinqi.common.satoken.utils.LoginHelper;
import com.xinqi.modules.course.clazz.dto.req.*;
import com.xinqi.modules.course.clazz.dto.rsp.ClassAuditContentResultDTO;
import com.xinqi.modules.course.clazz.service.ClassService;
import com.xinqi.modules.course.clazz.dto.rsp.ClassAllResultDTO;
import com.xinqi.modules.course.clazz.dto.rsp.ClassAppResultDTO;
import com.xinqi.modules.course.clazz.dto.rsp.ClassResultDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 班级表控制层
 *
 * @author: system
 * @date: 2023/01/17
 */
@Tag(name = "Web-班级", description = "班级")
@RestController
@RequestMapping("/web/class")
@RequiredArgsConstructor
public class ClassWebController {

    private final ClassService classService;

    @Operation(summary = "班级表-分页")
    @GetMapping("/page")
    public R<PageInfo<ClassResultDTO>> page(Param pageable, ClassQueryDTO dto) {
        PageInfo<ClassResultDTO> page = classService.page(pageable, dto);
        return R.data(page);
    }

    @Operation(summary = "班级表-详情")
    @GetMapping("/{classId}")
    public R<ClassResultDTO> findById(@PathVariable("classId") Long id) {
        return R.data(classService.findClassInfoByClassId(id));
    }

    @Operation(summary = "根据课程id查询其他班级列表")
    @GetMapping("/{classId}/find-other-class")
    public R<List<ClassResultDTO>> findOtherClasByClassId(@PathVariable("classId") Long classId) {
        List<ClassResultDTO> resultList = classService.findOtherClasByClassId(classId);
        return R.data(resultList);
    }

    @Operation(summary = "新增班级表")
    @PostMapping
    public R<Long> insert(@Validated @RequestBody ClassCreateDTO dto) {
        return R.data(classService.insert(dto));
    }

    @Operation(summary = "修改班级表")
    @PutMapping("/{classId}")
    public R<Boolean> update(@PathVariable("classId") Long classId, @Validated @RequestBody ClassUpdateDTO dto) {
        dto.setId(classId);
        return R.data(classService.updateById(dto));
    }

    @Operation(summary = "删除班级表")
    @DeleteMapping("/{classId}")
    public R<Boolean> delete(@PathVariable("classId") Long classId) {
        return R.data(classService.deleteById(classId));
    }

    @Operation(summary = "判断班级名称是否重复")
    @GetMapping("/is-exists-name")
    public R<Boolean> isExistsByName(ClassNameIsExistDTO dto) {
        return R.data(classService.isExistsByName(dto.getCourseId(), dto.getClassName(), dto.getId()));
    }

    @Operation(summary = "开启班级人数限制开关")
    @PutMapping("/{classId}/open-count-limit")
    public R<Boolean> openCountLimit(@PathVariable("classId") Long classId, ClassCountLimitUpdateDTO dto) {
        dto.setId(classId);
        return R.data(classService.openCountLimit(dto));
    }

    @Operation(summary = "关闭班级人数限制开关")
    @PutMapping("/{classId}/close-count-limit")
    public R<Boolean> closeCountLimit(@PathVariable("classId") Long classId) {
        return R.data(classService.closeCountLimit(classId), "设置成功");
    }

    @Operation(summary = "不允许退课")
    @PutMapping("/{classId}/forbid-drop-class")
    public R<Boolean> forbidDropClass(@PathVariable("classId") Long classId) {
        return R.data(classService.forbidDropClass(classId), "设置成功");
    }

    @Operation(summary = "允许退课")
    @PutMapping("/{classId}/allow-drop-class")
    public R<Boolean> allowDropClass(@PathVariable("classId") Long classId) {
        return R.data(classService.allowDropClass(classId), "设置成功");
    }

    @Operation(summary = "班级详情-查询")
    @GetMapping("/find")
    public R<ClassResultDTO> findDetail(ClassQueryDTO dto) {
        ClassResultDTO result = classService.findDetail(dto);
        return R.data(result);
    }

    @Operation(summary = "根据课程id-查询班级列表")
    @GetMapping("/find-list")
    public R<List<ClassResultDTO>> findListByCourseId(@RequestParam("courseId") Long courseId) {
        List<ClassResultDTO> resultList = classService.findByCourseId(courseId);
        return R.data(resultList);
    }

    @Operation(summary = "查询课程下的我管理的班级（剔除当前班级)")
    @GetMapping("/my-create-list")
    public R<List<ClassResultDTO>> findMyCreateClassByClassId(@RequestParam("classId") Long classId) {
        List<ClassResultDTO> resultList = classService.findMyCreateClassByClassId(classId, LoginHelper.getUserId());
        return R.data(resultList);
    }

    @Operation(summary = "未登录情况下-加课码查询班级")
    @GetMapping("/find-by-code-without-login")
    public R<ClassAppResultDTO> findByCodeWithoutLogin(@RequestParam("courseCode") String courseCode) {
        ClassAppResultDTO result = classService.findClassInfoByCodeSimple(courseCode);
        return R.data(result);
    }

    @Operation(summary = "加课码查询班级")
    @GetMapping("/find-by-code")
    public R<ClassAppResultDTO> findByCode(@RequestParam("courseCode") String courseCode) {
        ClassAppResultDTO result = classService.findClassInfoByCode(courseCode);
        return R.data(result);
    }

    @Operation(summary = "查询班级名称是否可编辑")
    @GetMapping("/{classId}/is-name-editable")
    public R<Boolean> isNameEditable(@PathVariable("classId") Long classId) {
        return R.data(classService.isNameEditable(classId));
    }

    @Operation(summary = "重置二维码/加课码")
    @PutMapping("/{classId}/reset-code")
    public R<ClassResultDTO> resetCode(@PathVariable("classId") Long classId) {
        return R.data(classService.resetCode(classId), "重置成功");
    }

    @Operation(summary = "停用二维码/加课码")
    @PutMapping("/{classId}/close-code")
    public R<ClassResultDTO> closeCode(@PathVariable("classId") Long classId) {
        return R.data(classService.closeCode(classId), "停用成功");
    }

    @Operation(summary = "启用二维码/加课码")
    @PutMapping("/{classId}/open-code")
    public R<ClassResultDTO> openCode(@PathVariable("classId") Long classId) {
        return R.data(classService.openCode(classId), "启用成功");
    }

    @Operation(summary = "退课-实际是退班级")
    @PostMapping("/{classId}/quit-class")
    public R<Boolean> quitClass(@PathVariable("classId") Long classId, @RequestParam("smsCode") String smsCode, @RequestParam("mobile") String mobile) {
        return R.data(classService.quitClass(LoginHelper.getXqLoginUser(), classId, smsCode, mobile), "退出成功");
    }

    @Operation(summary = "批量退课-实际是退班级")
    @PostMapping("/{classId}/batch-quit-class")
    public R<Integer> batchQuitClass(@PathVariable("classId") Long classId, @RequestBody List<Long> classStudentIds) {
        return R.data(classService.batchQuitClass(LoginHelper.getXqLoginUser(), classId, classStudentIds), "退出成功");
    }

    @Operation(summary = "根据ClassId获取对应课程、班级和学生信息")
    @PostMapping("/{classId}/info")
    public R<ClassAllResultDTO> classInfo(@PathVariable("classId") Long classId) {
        return R.data(classService.classAllMessage(classId));
    }

    @Operation(summary = "获取登录用户的审核内容信息")
    @GetMapping("/{classId}/audit-info")
    public R<ClassAuditContentResultDTO> getAuditContentInfo(@PathVariable("classId") Long classId) throws IOException {
        return R.data(classService.getAuditContentInfo(classId));
    }

}
